package com.wang.interview.N02_Singleton;

import java.util.concurrent.*;

/**
 * @author: wang
 * @date: 2021/7/12 20:56
 * @description:静态能保证单例，而如何在程序调用时才创建对象实例是懒汉式的核心
 * 1、上一种方法，较麻烦
 * 2、使用静态内部类
 * -- 静态内部类不会随着外部内的加载而加载，只会在被调用时加载，而且同一个类虚拟机只会加载一次。
 */
public class Lazy02 {
    private Pojo pojo;
    private Lazy02(){}
    //
    private static class Inner{
        public static Pojo pojo = new Pojo();
    }

    public static  Pojo getPojo(){
        return Inner.pojo;
    }
}

class TestLazy02{
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<Pojo> c = Lazy02::getPojo;
        ExecutorService p = Executors.newFixedThreadPool(2);
        Future<Pojo> t1 = p.submit(c);
        Future<Pojo> t2 = p.submit(c);
        Pojo p1 = t1.get();
        Pojo p2 = t2.get();
        System.out.println( p1 == p2);
        p.shutdown();
    }
}